package com.aa.adapter;

import java.util.List;

import com.aa.CallWaitActivity;
import com.aa.Common;
import com.aa.ContactsActivity;
import com.aa.R;
import com.aa.activity.ShareActivity;
import com.aa.activity.SipCallActivity;
import com.aa.contacts.KaguPhones;

import android.R.integer;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class FirentListAdapter extends BaseExpandableListAdapter {	
	private List<String> mGroupList;
	private List<List> mChildList;
	private Context mContext;
	
	public class ChildHolder{
		public TextView tvName;
		public TextView tvAlpha;
		public ImageView ivPhone;
		public TextView tvPhoneNumber;
		public View rltAlpha;
		public ImageView ivCallBack;
		public ImageView ivPhoneCall;
		public ImageView ivSendSMS;
		public ImageView ivShare;
		public RelativeLayout layContacDetail;
		public ImageView ivContactIcon;
	}
	
	private class GroupHolder{
		TextView mtvGroupName;
	}
	
	
	public FirentListAdapter(List<String> group,List<List> child, Context pContext){
		mGroupList = group;
		mChildList = child;
		mContext = pContext;
	}
	
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		
		return mChildList.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		ChildHolder _ChildHolder;
		
		if (convertView == null) {
			_ChildHolder = new ChildHolder();
			convertView = LayoutInflater.from(mContext).inflate(R.layout.contact_item, null);
			
			_ChildHolder.tvAlpha = (TextView)convertView.findViewById(R.id.tv_letters);
			//_Holder.ivPhone = (ImageView)convertView.findViewById(R.id.iv_image);
			_ChildHolder.tvName = (TextView)convertView.findViewById(R.id.tv_name);
			_ChildHolder.tvPhoneNumber = (TextView)convertView.findViewById(R.id.tv_number);
			_ChildHolder.rltAlpha = (View)convertView.findViewById(R.id.linearLayout8);
			_ChildHolder.ivCallBack = (ImageView)convertView.findViewById(R.id.iv_contact_callback);
			_ChildHolder.ivPhoneCall = (ImageView)convertView.findViewById(R.id.iv_contact_phonecall);
			_ChildHolder.ivSendSMS = (ImageView)convertView.findViewById(R.id.iv_contact_sendsms);
			_ChildHolder.ivShare = (ImageView)convertView.findViewById(R.id.iv_contact_share);
			_ChildHolder.layContacDetail = (RelativeLayout)convertView.findViewById(R.id.lay_contact_detail);
			_ChildHolder.ivContactIcon = (ImageView)convertView.findViewById(R.id.iv_contact_icon);
			int childId = R.layout.contact_item;
			int groudId = R.layout.group_list_item1;
			convertView.setTag(childId,childPosition);
			convertView.setTag(groudId, groupPosition);
			convertView.setTag(_ChildHolder);
			
		}else {
			_ChildHolder = (ChildHolder)convertView.getTag();			
		}
		
		 final KaguPhones _KaguPhones = (KaguPhones)getChild(groupPosition, childPosition);
		_ChildHolder.rltAlpha.setVisibility(View.GONE);
		_ChildHolder.layContacDetail.setVisibility(View.GONE);
		_ChildHolder.tvPhoneNumber.setVisibility(View.VISIBLE);
	
		
			if (_KaguPhones.getUser() == 1) {
				_ChildHolder.ivContactIcon.setVisibility(View.VISIBLE);
				if (_KaguPhones.getIsAgent() == 1) {
					if (_KaguPhones.getIsOnline() == 1) {
						_ChildHolder.ivContactIcon.setBackgroundResource(R.drawable.agent_icon1);
					}else {
						_ChildHolder.ivContactIcon.setBackgroundResource(R.drawable.agent_icon2);
					}
				}else {
					if (_KaguPhones.getIsOnline() == 1) {
						_ChildHolder.ivContactIcon.setBackgroundResource(R.drawable.user_icon1);
					}else {
						_ChildHolder.ivContactIcon.setBackgroundResource(R.drawable.user_icon2);
					}
				}
											
			}else {
				_ChildHolder.ivContactIcon.setVisibility(View.GONE);
			}
		
		
		
	
		
		_ChildHolder.tvName.setText(_KaguPhones.getDisplayName());
		_ChildHolder.tvPhoneNumber.setText(_KaguPhones.getPhoneNum());
		
		final String _Name = _KaguPhones.getDisplayName();
		final String _PhoneNum = _KaguPhones.getPhoneNum();
		
		_ChildHolder.ivCallBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				  if( _KaguPhones.getPhoneNum().equals(mContext.getString(R.string.number_private))){
				    	
				      String str = mContext.getString(R.string.app_net_tip1);
				      Toast.makeText(mContext, str, 3000).show();
				      return;
				    }
				    Common.iCallName = _Name;
				    if (_PhoneNum.startsWith("+86")) {
				    	Common.iCallNumber = _PhoneNum.substring(3, _PhoneNum.length());
					}else {
						 Common.iCallNumber = _PhoneNum;
					}
				
				Intent _Intent = new Intent();
				_Intent.setClass(mContext, CallWaitActivity.class);
				mContext.startActivity(_Intent);
			}
		});
		_ChildHolder.ivPhoneCall.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
					Intent _Intent = new Intent();
				    _Intent.setAction("android.intent.action.CALL");
				    _Intent.setData(Uri.parse("tel:" + _KaguPhones.getPhoneNum()));
				    mContext.startActivity(_Intent);
				
			}
		});
		_ChildHolder.ivSendSMS.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				Intent _Intent = new Intent(ContactsActivity.this, SipCallActivity.class);
//				startActivity(_Intent);
				
			}
		});
		
		_ChildHolder.ivShare.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				Intent _Intent = new Intent("android.intent.action.SENDTO", Uri.parse("smsto:" + _PhoneNum));
//				_Intent.putExtra("sms_body", mContext.getString(R.string.agent_fangfa_msg));
				Intent _Intent = new Intent(mContext, ShareActivity.class);
				Bundle bundle = new Bundle();
				String phoneNumber = Common.analysePhoneNumber(_PhoneNum);
				bundle.putString("PHONE", phoneNumber);
				_Intent.putExtras(bundle);
				mContext.startActivity(_Intent);
			}
		});
					
		return convertView;
		
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		
		return mChildList.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		
		return mGroupList.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		
		return mGroupList.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
			
		GroupHolder _GroupHolder;
		if (convertView == null) {
			_GroupHolder = new GroupHolder();
			convertView = LayoutInflater.from(mContext)
					.inflate(R.layout.group_list_item1, null);
			_GroupHolder.mtvGroupName = (TextView)convertView.findViewById(R.id.tv_groud_name);
			_GroupHolder.mtvGroupName.setText(mGroupList.get(groupPosition).toString());
			int groupId = R.layout.group_list_item1;
			int childId = R.layout.contact_item;
			convertView.setTag(groupId, groupPosition);
			convertView.setTag(childId, -1);
			convertView.setTag(_GroupHolder);
		}else {
			_GroupHolder = (GroupHolder)convertView.getTag();
		}
						
		return convertView;
	}

	@Override
	public boolean hasStableIds() {
		
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		
		return true;
	}

	

}
